package gui.popup;

import gui.App;
import gui.table.AutoTable;
import models.Client;
import models.Subscription;
import models.Workshop;

import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;

public class SubscriptionPopup_selectClient extends Popup<Subscription>{
	
	private AutoTable<Client> clientTable;

	public SubscriptionPopup_selectClient(App app) {
		super(app);
	}
	
	/**
	 * This function opens a subscription popup where the workshop is already selected. 
	 * @param workshop a Workshop that you want to subscribe for a workshop.
	 * @return if the popup is filled with valid data the return is an subscription. Else this functions return <code>null</code>.
	 */
	public Subscription open_selectClient(Workshop workshop) {
		Subscription subscription = open_Add();
		if (subscription != null) {
			subscription.setWorkshop(workshop);
		}
		return subscription;
	}

	/**
	 * This function creates an empty subscription for the adding popup
	 */
	@Override
	public Subscription createEmptyModel() {
		return new Subscription();
	}
	
	/**
	 * this function builds the form to subscribe a client for a workshop. It calls all pieces of the popup to create the whole popup.
	 */
	@Override
	public void buildForm(Composite formPanel, Subscription subscription) {
		setTitle_modelType("Inschrijving");
		
		formPanel.setLayout(new GridLayout(2, false));

		createFormItem_label(formPanel, "Klant");
		clientTable = createFormItem_client(formPanel, subscription.getClient());
	}

	/**
	 * This function checks if the workshop has reached its maximum capacity. if the maximum is reached a warning pop-up is showed.
	 */
	@Override
	public boolean isDataValid() {
		if (clientTable.getSelected() == null) {
			alertInvalidData("Selecteer een klant.");
			return false;
		}
		return true;
	}

	/**
	 * This function update's the given subscription with the client information
	 */
	@Override
	public void updateModel(Subscription subscription) {
		subscription.setClient(clientTable.getSelected());
	}
	
}
